import { Injectable } from '@angular/core';
import { Toast, ToastController } from 'ionic-angular';

@Injectable()
export class ToastService {

    toast: Toast;
    constructor(public toastCtrl: ToastController) { }

    /*** 创建
    * */
    create(message:string, ok = false, position= 'left', cssClass= 'office-default-toast',duration= 2000,text= 'OK') {
        this.closeToast();

        this.toast = this.toastCtrl.create({
            message: message,
            duration: ok ? null : duration,
            cssClass: cssClass,
            position: position,
            showCloseButton: ok,
            closeButtonText: text,
            dismissOnPageChange: true
        });
        this.toast.present();
    }

    /*** 创建居中 'office-center-toast' middle
    * */
    createCenterToast(message:string,duration= 2000, cssClass= 'office-center-toast')
    {
      this.create(message,false,'middle',cssClass, duration);
    }

    /*** 关闭
    * */
    closeToast(): Promise<any>
    {
      if (this.toast)
        return this.toast.dismiss();
    }
}
